package com.spring.sunflower

import com.spring.sunflower.base.BaseObserver
import com.spring.sunflower.base.BasePresenter
import com.spring.sunflower.bean.*
import com.spring.sunflower.constant.Constant
import io.reactivex.rxjava3.core.Observable

/**
 * Created by Kevin on 2021/8/18<br/>
 *
 * Describe:<br/>
 */
class MainPresenter(baseView: MainView) : BasePresenter<MainView>(baseView) {
    fun keepOnline() {
        val observable: Observable<CommonBean> =
            requestDataSource.keepOnline()
        addDisposable(observable, object : BaseObserver<CommonBean>() {
            override fun onSuccess(bean: CommonBean?) {
            }

            override fun onError(status: Int, msg: String) {
            }

        })
    }

    fun querySignInRecord() {
        val observable = requestDataSource.querySignInRecord()
        addDisposable(observable, object : BaseObserver<SignInBean>() {
            override fun onSuccess(bean: SignInBean) {
                if (bean.datas != null) {
                    baseView.hasSigned()
                } else {
                    baseView.noSign()
                }
            }

            override fun onError(status: Int, msg: String) {}
        })
    }

    fun signIn() {
        val observable = requestDataSource.signIn()
        addDisposable(observable, object : BaseObserver<SignInBean>() {
            override fun onSuccess(bean: SignInBean) {
                baseView.signSuccess(bean.datas)
            }

            override fun onError(status: Int, msg: String) {
                baseView.signFailed(msg)
            }
        })
    }

    fun getSayHelloList() {
        saveField(Constant.KEY_SAY_HELLO, true)
        val observable = requestDataSource.getSayHelloList()
        addDisposable(observable, object : BaseObserver<HelloBean>() {
            override fun onSuccess(bean: HelloBean) {
                if (bean.datas != null) {
                    baseView.sayHello(bean.datas)
                } else {
                    baseView.noSayHello()
                }
            }

            override fun onError(status: Int, msg: String) {
            }
        })
    }

    fun sayHello(anchorIdList: String) {
        val observable = requestDataSource.sayHello(anchorIdList)
        addDisposable(observable, object : BaseObserver<BlackHasBean>() {
            override fun onSuccess(bean: BlackHasBean) {
                baseView.sayHelloSuccess()

            }

            override fun onError(status: Int, msg: String) {
                baseView.sayHelloFailed()
            }
        })
    }

    fun getNewGift() {
        saveField(Constant.KEY_SAY_HELLO, true)
        val observable = requestDataSource.newGift
        addDisposable(observable, object : BaseObserver<NewGiftBagBean>() {
            override fun onSuccess(bean: NewGiftBagBean) {
                if (bean.datas != null) {
                    baseView.showNewGift(bean.datas)
                } else {
                    baseView.showNewGiftFailed()
                }

            }

            override fun onError(status: Int, msg: String) {
                baseView.showNewGiftFailed()
            }
        })
    }

    fun getLatestVersion() {
        val observable = requestDataSource.latestVersion
        addDisposable(observable, object : BaseObserver<VersionBean>(baseView) {
            override fun onSuccess(bean: VersionBean) {
                val datas = bean.datas
                if (datas != null) {
                    baseView.getVersionSuccess(datas)
                } else {
                    baseView.getVersionWithNull()
                }
            }

            override fun onError(status: Int, msg: String) {}
        })
    }
}
